//
//  PhotoBrowserViewController.m
//  tour
//
//  Created by 卡忙科技 on 16/6/30.
//  Copyright © 2016年 Jushi. All rights reserved.
//

#import "PhotoBrowserViewController.h"
#import "PhotoCollectionViewCell.h"

@interface PhotoBrowserViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,weak) UICollectionView *collection;
@property (nonatomic,weak) UILabel *countLab;

@end

@implementation PhotoBrowserViewController

static NSString * const reuseIdentifier = @"PhotoCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.itemSize = CGSizeMake(ScreenW, ScreenH);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    collection.dataSource = self;
    collection.delegate = self;
    collection.pagingEnabled = YES;
    [collection registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collection = collection;
    [self.view addSubview:collection];
    [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    UILabel *countLab = [[UILabel alloc]initWithFrame:CGRectMake(0, ScreenH-30, ScreenW, 20)];
    countLab.backgroundColor = [UIColor clearColor];
    countLab.textColor = [UIColor whiteColor];
    countLab.font = Font(14);
    countLab.textAlignment = NSTextAlignmentCenter;
    countLab.text = [NSString stringWithFormat:@"%zd/%zd",self.index+1,self.picArray.count];
    [self.view addSubview:countLab];
    self.countLab = countLab;
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.imageName = self.picArray[indexPath.item];
    cell.dismissBlock = ^(){
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.picArray.count;
    self.countLab.text = [NSString stringWithFormat:@"%d/%ld",page+1,self.picArray.count];
}

@end
